;
(function(factory) {
	layui.use(['layer', 'laypage', 'table', 'form'], function() {
		factory(jQuery, window, document, layui);
	});
}(function($, window, document, layui) {
	$(function() {
		funOp.init();
		funOp.tableOn();
	});

	var funOp = {
		limit: 10,
		init: function() {

			$('#seach_submit').click(function() {
				funOp.queryRole(1);
				return false;
			});
			
			funOp.tableInit();
		},

		tableInit: function() {
			layui.table.render({
				elem: '#menu_table',
				toolbar: '#toolbarDemo',
				limit: funOp.limit,
				id: 'menu_table',
				cols: [
					[/**{
						fixed: 'left',
						title: '操作',
						width: 200,
						align:'center',
						toolbar: '#barDemo'
					},*/ {
						field: 'id',
						title: 'ID',
						width: 50,
					}, {
						field: 'busiSerial',
						title: '交易流水号',
						width: 200
					}, {
						field: 'transDate',
						title: '交易日期',
						width: 100
					}, {
						field: 'transTime',
						title: '交易时间',
						width: 100
					}, {
						field: 'city',
						title: '地区',
						width: 100
					}, {
						field: 'userNo',
						title: '用户号',
						width: 200
					}, {
						field: 'amt',
						title: '缴费金额',
						width: 100
					}, {
						field: 'cardNo',
						title: '缴费账号',
						width: 200
					}, {
						field: 'transType',
						title: '交易状态',
						width: 100
					}, {
						field: 'remark',
						title: '备注',
						width: 100
					}]
				],
				data: []
			});
		},

		tableOn: function() {
			//头工具栏事件
			layui.table.on('toolbar(menu_table)', function(obj){
				// var checkStatus = table.checkStatus(obj.config.id);
				switch(obj.event){
				case 'add':
					funOp.addRole();
				break;
				}
			});

			//监听行工具事件
			layui.table.on('tool(menu_table)', function(obj){
				var data = obj.data;
				if(obj.event === 'detail'){
					funOp.roleDeatil(obj.data);
				} else if(obj.event === 'del'){
					funOp.deleteRole(obj.data);
				} else if(obj.event === 'edit'){
					funOp.editRole(obj.data);
				}
			});
		},

		tableReload: function(pageIndex, total, data) {
			layui.table.reload('menu_table', {
				data: data
			});
			layui.laypage.render({
				elem: 'menu_page',
				limit: funOp.limit,
				curr: pageIndex,
				groups: 2,
				count: total,
				layout: ['count', 'prev', 'page', 'next'],
				jump: function(obj, first) {
					if (!first) {
						funOp.queryRole(obj.curr);
					}
				}
			});
		},

		queryRole: function(pageIndex) {
			var params = layui.form.val('seach_form');
			var where = ' where 0=0 ';
			var paramList = [];
			var pageParamList = [];
			$.each(params, function(key, value) {
				if(!value || value == ''){
					return true;
				}
				where += ' and ' + key + ' like concat("%", ? ,"%")';
				paramList.push(value);
			});
			where += ' and r.`trans_type` is not null';
			pageParamList = paramList.concat([(pageIndex - 1) * funOp.limit, (pageIndex) * funOp.limit]);
			
			var c = 'SELECT count(0) num from jsdf_translist r'+ where;
			var s = 'SELECT r.id,r.busi_serial,r.trans_date,r.trans_time,r.city,r.user_no,r.amt,r.card_no,r.trans_type,r.remark'
			+' from jsdf_translist r' + where + ' limit !,! ';
			
			var cObj = {
				sql: $.fn.commUtil.getSqlStr(c, paramList)
			};
			var sObj = {
				sql: $.fn.commUtil.getSqlStr(s, pageParamList)
			};
			$.newAjax({
				url: $.fn.commUtil.constant.baseUrl + '/dealSql',
				data: JSON.stringify(cObj),
				success: function(res) {
					// console.info(res)
					if (res.code != 0 || res.data.length == 0 || res.data[0] == 0) {
						alert(res.msg);
						return;
					}
					var total = res.data[0].num;
					$.newAjax({
						url: $.fn.commUtil.constant.baseUrl + '/dealSql',
						data: JSON.stringify(sObj),
						success: function(res) {
							// console.info(res)
							if (res.code != 0) {
								alert(res.msg);
								return;
							}
							funOp.tableReload(pageIndex, total, res.data);
						}
					});
				}
			});
		},

		/* roleDeatil: function(params, data) {
			layer.open({
				type: 1,
				title: '查看角色',
				area: ['600px','400px'],
				maxmin: true,
				content: $('#detailMsg')
			}); 
			//给表单赋值
			layui.form.val("detail", {
				"transno": params.transno,
				"transdate": params.transdate,
				"city": params.city,
				"userno": params.userno,
				"amt": params.amt,
				"cardno": params.cardno,
				"transtype": params.transtype,
				"remark": params.remark,
			});
		},
 */
		/* addRole: function() {
			layer.open({
				type: 1,
				title: '新增角色',
				area: ['600px','400px'],
				maxmin: true,
				content: $('#addMsg')
			});
			//自定义验证规则
			// layui.form.verify({
			// 	name: function(value){
			// 	if(value.length < 5){
			// 		return '标题至少得5个字符啊';
			// 	}
			// 	}
			// });
			layui.form.on("submit(addSub)", function(data){
				// alert(JSON.stringify(data.field));				
				var a = 'select * from user where username = ? and del_flag = 0';
				var aParam = [];
				//获取登录人
				var loginUser = $.fn.commUtil.getLoginUser();
				var username = loginUser.username;
				aParam.push(username);

				var aObj = {
					sql: $.fn.commUtil.getSqlStr(a, aParam)
				};
				$.newAjax({
					url: $.fn.commUtil.constant.baseUrl + '/dealSql',
					data: JSON.stringify(aObj),
					success: function(res) {
						if (res.code != 0) {
							layer.msg('添加失败！');
							return;
						}
						var c = 'insert into role(`name`,type,remark,create_user,create_date,del_flag) values (?,?,?,?,?,0)';
						var cParam = [];

						var date = (new Date()).Format("yyyy-MM-dd hh:mm:ss");
						cParam.push(data.field.name);
						cParam.push(data.field.type);
						cParam.push(data.field.remark);
						cParam.push(res.data[0].id);
						cParam.push(date);

						var cObj = {
							sql: $.fn.commUtil.getSqlStr(c, cParam)
						};
						$.newAjax({
							url: $.fn.commUtil.constant.baseUrl + '/dealSql',
							data: JSON.stringify(cObj),
							success: function(res) {
								if (res.code != 0) {
									layer.msg('添加失败！');
									return;
								}
								layer.msg(res.msg);
								layer.closeAll();
								funOp.queryRole(1);
							}
						});
					}
				});
				return false;
			});
		}, */

		/* editRole: function(params, data) {
			//给表单赋值
			layui.form.val("edit", {
				"id": params.id,
				"name": params.name,
				"type": params.type,
				"createUser": params.createUser,
				"createDate": params.createDate,
				"remark": params.remark
			});
			// alert(JSON.stringify(params));
			layer.open({
				type: 1,
				title: '编辑角色',
				area: ['600px','400px'],
				maxmin: true,
				content: $('#editMsg')
			}); 
			layui.form.on("submit(editSub)", function(data){
				// alert(JSON.stringify(data.field));
				var s = 'update role set name = ?,type = ?,remark = ? where id = ?';
				var param = [];
				param.push(data.field.name);
				param.push(data.field.type);
				param.push(data.field.remark);
				param.push(data.field.id);
				var obj = {
					sql: $.fn.commUtil.getSqlStr(s, param)
				};
				$.newAjax({
					url: $.fn.commUtil.constant.baseUrl + '/dealSql',
					data: JSON.stringify(obj),
					success: function(res) {
						if (res.code != 0) {
							layer.msg('修改失败！');
							return;
						}
						// alert(JSON.stringify(res));
						layer.msg(res.msg);
						layer.closeAll();
						funOp.queryRole(1);
					}
				});
				return false;
			});
		}, */

		/* deleteRole: function(obj, data) {
			var id = obj.id;
			layer.confirm('确认删除吗？', function(index){
				// alert(JSON.stringify(obj));
				var c = 'update role set del_flag = 1 where id = ?';
				var param = [];
				param.push(id);
				var obj = {
					sql: $.fn.commUtil.getSqlStr(c, param)
				};
				$.newAjax({
					url: $.fn.commUtil.constant.baseUrl + '/dealSql',
					data: JSON.stringify(obj),
					success: function(res) {
						if (res.code != 0) {
							layer.msg('删除失败！');
							return;
						}
						layer.msg(res.msg);
						funOp.queryRole(1);
					}
				});
			});
		} */
	};
}));
